package com.cn.delan.web.mvc.admin.action;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import com.cn.delan.core.util.StringUtil;
import com.cn.delan.web.mvc.admin.bean.UserBean;
import com.cn.delan.web.mvc.admin.service.IUserService;
import com.cn.delan.web.mvc.core.action.AbstractAction;
/**
 * 用户管理
 * @author BingGo
 *
 */
public class UserManageAction extends AbstractAction {
	private IUserService<UserBean> userService;

	public IUserService<UserBean> getUserService() {
		return userService;
	}

	public void setUserService(IUserService<UserBean> userService) {
		this.userService = userService;
	}

	@Override
	public ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pageNo = this.getParameter("page");
		String rows = this.getParameter("rows");
		String sort = this.getParameter("sort");
		String order = this.getParameter("order");
		String search_col_1 = this.getParameter("search_col_1");
		String search_val_1 = this.getParameter("search_val_1");
		String search_col_2 = this.getParameter("search_col_2");
		String search_val_2 = this.getParameter("search_val_2");
		if(pageNo==null){
			//获取用户列表页面框架
			return this.getView(INPUT);
		}else{
			//获取JSON格式的用户列表
			PrintWriter out = this.getWriter();
			String endSQL = "";
			ArrayList<Object> params = new ArrayList<Object>();
			//第一检索参数
			if(!StringUtil.isEmpty(search_col_1)){
				if(StringUtil.isEmpty(endSQL)){
					endSQL += " where";
				}else{
					endSQL += " and";
				}
				endSQL += " "+search_col_1+" like ?";
				params.add("%"+search_val_1+"%");
			}
			//第二检索参数
			if(!StringUtil.isEmpty(search_col_2)){
				if(StringUtil.isEmpty(endSQL)){
					endSQL += " where";
				}else{
					endSQL += " and";
				}
				endSQL += " "+search_col_2+" like ?";
				params.add("%"+search_val_2+"%");
			}
			//排序
			if(sort!=null){
				endSQL += " order by "+sort+" "+order;
			}
			JSONObject users = this.userService.queryToJson(endSQL, params.toArray(new Object[params.size()]),Integer.parseInt(pageNo),Integer.parseInt(rows));
			//用户列表的json格式
			out.println(users.toJSONString());
			return null;
		}
	}

}
